﻿namespace HiMap.MapControls.Tools
{
    using HiMap.Geometry;
    using HiMap.MapControls;
    using System;
    using System.Drawing;
    using System.Windows.Forms;

    public class ToolPan : ToolClass
    {
        private bool isMouseDown;
        private int oldx;
        private int oldy;
        private Point staPT;
        private int stax;
        private int stay;
        private int x;
        private int y;

        public ToolPan()
        {
            try
            {
                this.stax = 0;
                this.stay = 0;
                this.oldx = 0;
                this.oldy = 0;
                this.isMouseDown = false;
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }

        protected override void OnMouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                this.staPT.X = e.X;
                this.staPT.Y = e.Y;
                this.stax = e.X;
                this.stay = e.Y;
                this.stax -= this.oldx;
                this.stay -= this.oldy;
                this.isMouseDown = true;
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }

        protected override void OnMouseMove(object sender, MouseEventArgs e)
        {
            try
            {
                if (this.isMouseDown)
                {
                    this.x = e.X - this.stax;
                    this.y = e.Y - this.stay;
                    base.Map.MapRefresh(this.x, this.y);
                    this.oldx = this.x;
                    this.oldy = this.y;
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }

        protected override void OnMouseUp(object sender, MouseEventArgs e)
        {
            try
            {
                if (this.isMouseDown)
                {
                    this.stax = 0;
                    this.stay = 0;
                    this.oldx = 0;
                    this.oldy = 0;
                    double num = base.Map.Map.Envelope.MaxX - base.Map.Map.Envelope.MinX;
                    double maxY = base.Map.Map.Envelope.MaxY;
                    double minY = base.Map.Map.Envelope.MinY;
                    IEnvelope envelope = new EnvelopeClass();
                    envelope.MinX = base.Map.Map.Envelope.MinX - ((num / ((double) base.Map.Size.Width)) * this.x);
                    envelope.MaxX = base.Map.Map.Envelope.MaxX - ((num / ((double) base.Map.Size.Width)) * this.x);
                    envelope.MinY = base.Map.Map.Envelope.MinY + ((num / ((double) base.Map.Size.Width)) * this.y);
                    envelope.MaxY = base.Map.Map.Envelope.MaxY + ((num / ((double) base.Map.Size.Width)) * this.y);
                    base.Map.Map.Envelope = envelope;
                    base.Map.MapRefresh();
                    this.isMouseDown = false;
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }

        protected override void OnToolCreate(MapControl Map)
        {
            try
            {
                base.OnToolCreate(Map);
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }
    }
}

